package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.system.domain.WxGroupSendingSchedule;
import com.ruoyi.system.domain.WxUser;
import com.ruoyi.system.service.IWeixinService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 微信定时群发
 * @author admin
 *
 */
@Controller
@RequestMapping("/system/wxSengdingSchdule")
public class WxGroupSendingScheduleController extends BaseController{
	
	private String prefix = "system/wxsengdingschdule";
	
	@Autowired
	private IWeixinService weixinService;
	
	/**
	 * 视图页面展示
	 * @return
	 */
	@RequiresPermissions("system:wxSengdingSchdule:view")
    @GetMapping()
    public String index(){
        return prefix + "/sendingschdule";
    }

    /**
	 * 微信定时群发列表页数据
	 * @param sendingSchedule
	 * @return
	 */
	@RequiresPermissions("system:wxSengdingSchdule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxGroupSendingSchedule sendingSchedule){
        startPage();
        List<WxGroupSendingSchedule> list = weixinService.selectWxGroupSendingScheduleList(sendingSchedule);
        return getDataTable(list);
    }

    /**
     * 添加定时微信群发
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
        // 获取微信号列表
        mmap.put("codes", weixinService.selectWxCodes());
        // 获取模板列表
        mmap.put("templates", weixinService.selectWxSendMsgModels());
        return prefix + "/add";
    }

    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        // 获取微信号列表
        mmap.put("codes", weixinService.selectWxCodes());
        // 获取模板列表
        mmap.put("templates", weixinService.selectWxSendMsgModels());

        mmap.put("sendSchedule", weixinService.getSendingScheduleById(id));

        return prefix + "/edit";
    }


    /**
     * 新增
     */
    @RequiresPermissions("system:wxSengdingSchdule:add")
    @Log(title = "新增微信定时群发", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(WxGroupSendingSchedule sendingSchedule){
        sendingSchedule.setCreateAt(new Date());
        return toAjax(weixinService.insertSendingSchedule(sendingSchedule));
    }

    /**
     * 更新
     */
    @RequiresPermissions("system:wxSengdingSchdule:edit")
    @Log(title = "更新微信定时群发", businessType = BusinessType.INSERT)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(WxGroupSendingSchedule sendingSchedule){
        return toAjax(weixinService.updateSendingSchedule(sendingSchedule));
    }


    @RequiresPermissions("system:wxSengdingSchdule:remove")
    @Log(title = "微信定时群发管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try{
            return toAjax(weixinService.deleteSengdingSchduleByIds(ids),"删除数据成功");
        } catch (Exception e){ return error(e.getMessage()); }
    }

}
